<?php

    class CHttpClient
    {
		var $m_hSocket;
    
        static function &GetInstance()
		{
		    global $g_hObjects;
		    static $m_hApp = NULL;
						    
		    if(!isset($m_hApp))
		    {
		        $m_hApp = new CHttpClient();
		        $g_hObjects[] = $m_hApp;
				$m_hApp->OnInit();												    
		    }
																	
		    return $m_hApp;
		}
	
		function OnInit()
		{
		}
	
		function OnDestroy()
		{
	    
		}	
	
		function GetToFile($szUrl,$iMaxSize,&$szServerLocation = 0,$iTimeout = 20)
		{
		    $aUrl = @parse_url($szUrl);
		
		    if($aUrl['host'] == '' ) { return false; }
		
		    $aUrl['path'] = isset($aUrl['path']) ? $aUrl['path'] : '/';
		    $aUrl['port'] = isset($aUrl['port']) ? $aUrl['port'] : 80;
		    
		    
		    $szQuery = "GET ".$aUrl['path']." HTTP/1.0\r\n";
		    $szQuery.= "Host: ".$aUrl['host']."\r\n";
		    $szQuery.= "User-Agent: bts\r\n";
		    $szQuery.= "Connection: Close\r\n\r\n";
		    
		    $szIp = gethostbyname($aUrl['host']);
		    
		    $hSocket = @fsockopen($szIp,$aUrl['port'],$iErrNo,$szStr,$iTimeout);
		    
		    @fputs($hSocket,$szQuery);
		    
		    $szOutput = '';
		    
		    while( !@feof($hSocket) )
		    {
			$szOutput .= @fgets($hSocket,4096);	
			
			if(strpos($szOutput,"\r\n\r\n") > 0)
			    break;
		    }
		    
		    $szOutput = strstr($szOutput,"Content-Length:");
		    $szOutput = strstr($szOutput," ");
		    $szOutput = substr($szOutput,0,stripos($szOutput,"\r\n"));
		    
		    $iContentLen = (int) trim($szOutput);
		    
		    if($iContentLen > $iMaxSize)
		    {
			fclose($hSocket);
			return false;
		    }
		
		    $szRandStr = md5(uniqid(rand(),time()));
		    $szServerLocation = UPLOAD_DIR.'/'.$szRandStr;
			    
		
		    $hDestFile = fopen($szServerLocation,'w');
		    
		    if( !$hDestFile ) { return false; }
		
		
		    while( !@feof($hSocket) )
		    {
			$szOutput = @fgets($hSocket,4096);
			fwrite($hDestFile,$szOutput);
		    }
		    
		    fclose($hSocket);
		    fclose($hDestFile);
		    	    
		    return true;
		}
	
		function PostSSL($szUrl,$aVars,$iTimeout = 20)
		{
		    $aUrl = @parse_url($szUrl);
		    
		    if($aUrl['host'] == '' ) { return false; }
		    
		    $aUrl['path'] = isset($aUrl['path']) ? $aUrl['path'] : '/';
		    $aUrl['port'] = isset($aUrl['port']) ? $aUrl['port'] : '443';
		    
		    
		    //convert our array to post request
		    $szVars = '';
		    
		    foreach($aVars as $szKey => &$szVal)
		    {
				$szVars .= urlencode($szKey).'='.urlencode($szVal).'&';
		    }
		    
		    $szVars   = substr($szVars,0,-1);	//wycinamy ostatni &
		    $iVarsLen = strlen($szVars);		//strlen po zmiennych
		    
		    //http request
		    $szHeader = "POST ".$aUrl['path']." HTTP/1.0\r\n";
		    $szHeader.= "Host: ".$aUrl['host']."\r\n";
		    $szHeader.= "User-Agent: gorilla\r\n";
		    $szHeader.= "Content-Type: application/x-www-form-urlencoded\r\n";
		    $szHeader.= "Content-Length: ".$iVarsLen."\r\n";
		    $szHeader.= "Connection: Close\r\n\r\n";
		    
		    $szQuery = $szHeader.$szVars."\r\n";
		    
		    //let's help fsockopen do the job
		    $szIp = @gethostbyname($aUrl['host']);
		    
		    //opening ssl connection
		    $hSocket = @fsockopen('ssl://'.$szIp,$aUrl['port'],$iErrNo,$szErrorStr,$iTimeout);
		    
		    if(!$hSocket)
		    {
				return false;
		    }
		    
		    fwrite($hSocket,$szQuery);
		    
		    $szOutput = '';
		    
		    while( !@feof($hSocket) ) { $szOutput .= @fgets($hSocket,8192); }
		    
		    fclose($hSocket);
		    
		    $iHeaderStop = strpos($szOutput,"\r\n\r\n");
		    
		    $szOutput = substr($szOutput,$iHeaderStop + 4);
		    
		    return trim($szOutput);
		}
		
		//wysyla zapytanie post pod zadany url
		//
		// $szUrl    - url pod ktory chcemy wyslac zadanie
		// $aVars    - tablica asocjacyjna z wartosciami w formacie array('key' => 'value');
		// $iTimeout - czas oczekiwania na polaczenie 
		static function Post($szUrl,$aVars,$iTimeout = 20)
		{
			$aUrl = @parse_url($szUrl);
		    
		    if($aUrl['host'] == '' ) { return false; }
		    
		    
		    $aUrl['path'] = isset($aUrl['path']) ? $aUrl['path'] : '/';
		    $aUrl['port'] = isset($aUrl['port']) ? $aUrl['port'] : '80';
		    
		    
		    //ciag txt ze zmiennymi
		    $szVars = '';
		    
		    foreach($aVars as $szKey => &$szVal)
		    {
				$szVars .= urlencode($szKey).'='.urlencode($szVal).'&';
		    }
		    
		    $szVars   = substr($szVars,0,-1);
		    $iVarsLen = strlen($szVars);
		    
		    //http request
		    $szHeader = "POST ".$aUrl['path']." HTTP/1.0\r\n";
		    $szHeader.= "Host: ".$aUrl['host']."\r\n";
		    $szHeader.= "User-Agent: gorilla\r\n";
		    $szHeader.= "Content-Type: application/x-www-form-urlencoded\r\n";
		    $szHeader.= "Content-Length: ".$iVarsLen."\r\n";
		    $szHeader.= "Connection: Close\r\n\r\n";
		    
		    $szQuery = $szHeader.$szVars."\r\n";
		    
		    //pobierz nazwe hosta z dns'a
		    $szIp = @gethostbyname($aUrl['host']);
		    
		    //otwieramy polaczenie do hosta docelowego 
		    $hSocket = @fsockopen($szIp,$aUrl['port'],$iErrNo,$szErrorStr,$iTimeout);
		    
		    if(!$hSocket)
				return false;
		    
			//zapisujemy zadanie do socket'a
		    fwrite($hSocket,$szQuery);
		    
		    //bufor na rezultaty
		    $szOutput = '';
		    
		    while( !@feof($hSocket) )
		    	$szOutput .= @fgets($hSocket,8192);
		    
		    fclose($hSocket);
		    
		    
		    $szOut = substr($szOutput,strpos($szOutput,"\r\n\r\n") + 4);
		    
		   	return trim($szOut); 
		}
    }
?>